package net.shopxx.action.member;

import java.io.File;

import javax.annotation.Resource;

import net.shopxx.entity.FlagshipStore;
import net.shopxx.entity.Member;
import net.shopxx.service.FlagshipStoreService;
import net.shopxx.util.CommonUtil;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 旗舰店
 */

@ParentPackage("member")
public class FlagshipAction extends BaseMemberAction {

	private static final long serialVersionUID = -7786508966240073537L;

	private FlagshipStore store;
	
	private File banner;
	private String bannerFileName;
	
	private File logo;
	private String logoFileName;
	
	

	@Resource
	private FlagshipStoreService flagshipStoreService;

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		id= getLoginMember().getFlagshipStore().getId();
		store = flagshipStoreService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		// 根据位置排序
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FlagshipStore.class);
//		detachedCriteria.addOrder(Order.asc("position"));
//		detachedCriteria.addOrder(Order.asc("orderList"));
		pager = flagshipStoreService.findByPager(pager, detachedCriteria);
//		pager = flagshipStoreService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() {
		flagshipStoreService.delete(ids);
		redirectionUrl = "flagshipStore!list.action";
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "store.store_name", message = "店铺名称不允许为空!")
		}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		store.setMember(getLoginMember());
		if (logo != null) {
			String savePath = validateFile(logo,logoFileName);
			if(ERROR.equals(savePath))
			{
				return ERROR;
			}
			 
			store.setStore_logo(savePath);
		}
		if (banner != null) {
			String savePath = validateFile(banner,bannerFileName);
			if(ERROR.equals(savePath))
			{
				return ERROR;
			}
			store.setStore_banner(savePath);
		}
		flagshipStoreService.save(store);
		redirectionUrl = "flagship!edit.action";
		getResponse().sendRedirect(redirectionUrl);
		return null;
	}

	// 更新
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "store.store_name", message = "店铺名称不允许为空!")
		}
	)
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		FlagshipStore persistent = flagshipStoreService.load(id);
		if (logo != null) {
			String savePath = validateFile(logo,logoFileName);
			if(ERROR.equals(savePath))
			{
				return ERROR;
			}
			persistent.setStore_logo(savePath);
		}
		if (banner != null) {
			String savePath = validateFile(banner,bannerFileName);
			if(ERROR.equals(savePath))
			{
				return ERROR;
			}
			persistent.setStore_banner(savePath);
		}
		persistent.setStore_name(store.getStore_name());
		flagshipStoreService.update(persistent);
		redirectionUrl = "flagship!edit.action?id="+persistent.getId();
		getResponse().sendRedirect(redirectionUrl);
		return null;
	}

	public FlagshipStore getStore() {
		return store;
	}

	public void setStore(FlagshipStore flagshipStore) {
		this.store = flagshipStore;
	}

	public FlagshipStoreService getFlagshipStoreService() {
		return flagshipStoreService;
	}

	public void setFlagshipStoreService(FlagshipStoreService flagshipStoreService) {
		this.flagshipStoreService = flagshipStoreService;
	}

	public File getBanner() {
		return banner;
	}

	public void setBanner(File banner) {
		this.banner = banner;
	}

	public File getLogo() {
		return logo;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public String getBannerFileName() {
		return bannerFileName;
	}

	public void setBannerFileName(String bannerFileName) {
		this.bannerFileName = bannerFileName;
	}

	public String getLogoFileName() {
		return logoFileName;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}

}